//
//  UIAlertController+Alert.m
//  MGPublic
//
//  Created by lhongl on 2025/10/15.
//

#import "UIAlertController+Alert.h"
#import "MGNavigator.h"
#import "MGEmpty.h"
@implementation UIAlertController (Alert)

/// 设置标题
- (UIAlertController *(^)(NSMutableAttributedString *title))attrTitle {
    return ^(NSMutableAttributedString *title){
        [self setValue:title forKey:@"attributedTitle"];
        return self;
    };
}

/// 设置内容
- (UIAlertController *(^)(NSMutableAttributedString *message))attrMessage {
    return ^(NSMutableAttributedString *message) {
        [self setValue:message forKey:@"attributedMessage"];
        return self;
    };
}

/// 添加默认Action
/// - Parameters:
///   - titile: 标题
///   - handler: 事件
- (void)addActionWithDefault:(NSString *)title
                 handler:(void (^ __nullable)(UIAlertAction *action))handler {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault
                                                   handler:handler];
    [self addAction:action];
}

/// 添加多个Action
/// - Parameters:
///   - titles: 标题
///   - handler: 回调
- (void)addActionsWithTitles:(NSArray <NSString *> *)titles
                     handler:(void (^ __nullable)(UIAlertAction *action,NSInteger index))handler {
    if (MGEmpty.isEmptyArray(titles)) {
        return;
    }
    [titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIAlertAction* action = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
            if (handler) {
                handler(action,idx);
            }
        }];
        [self addAction:action];
    }];
}

/// 添加Destructive的Action
/// - Parameters:
///   - titile: 标题
///   - handler: 事件
- (void)addActionWithDestructive:(NSString *)title
                     handler:(void (^ __nullable)(UIAlertAction *action))handler {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDestructive
                                                   handler:handler];
    [self addAction:action];
}


/// 添加默认Action
/// - Parameters:
///   - titile: 标题
///   - handler: 事件
- (void)addActionWithTitle:(NSString *)title
                     color:(UIColor *)color
                   handler:(void (^ __nullable)(UIAlertAction *action))handler; {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault
                                                   handler:handler];
    [action setValue:color forKey:@"titleTextColor"];
    [self addAction:action];
}

/// 添加多个Action
/// - Parameters:
///   - titles: 标题
///   - handler: 回调
- (void)addActionWithTitles:(NSArray <NSString *> *)titles
                     color:(UIColor *)color
                   handler:(void (^ __nullable)(UIAlertAction *action,NSInteger index))handler {
    if (MGEmpty.isEmptyArray(titles)) {
        return;
    }
    [titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIAlertAction* action = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
            if (handler) {
                handler(action,idx);
            }
        }];
        [action setValue:color forKey:@"titleTextColor"];
        [self addAction:action];
    }];
}

/// 添加取消的Action
/// - Parameters:
///   - titile: 标题
///   - handler: 事件
- (void)addActionWithCancel:(NSString *)title
                handler:(void (^ __nullable)(UIAlertAction *action))handler {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel
                                                   handler:handler];
    [self addAction:action];
}

/// 添加取消的Action
/// - Parameters:
///   - titile: 标题
- (void)addActionWithCancel:(NSString *)title {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel
                                                   handler:NULL];
    [self addAction:action];
}

/// 添加取消的Action
/// - Parameters:
///   - color: 颜色
///   - titile: 标题
- (void)addActionWithCancel:(NSString *)title
                      color:(UIColor *)color
                    handler:(void (^ __nullable)(UIAlertAction *action))handler {
    UIAlertAction* action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel
                                                   handler:NULL];
    [action setValue:color forKey:@"titleTextColor"];
    [self addAction:action];
}


/// 显示Alert
- (void)showAlert {
    [MGNavigator presentViewController:self];
}

@end
